﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Red_Cross_Philippines;

namespace Philippine_National_Red_Cross
{
    public partial class Login : Form
    {

        Blood_Bank_System BloodSystem = new Blood_Bank_System();

        public Login()
        {
            InitializeComponent();
            chkShowPass.CheckedChanged += new EventHandler(chkShowPass_CheckedChanged);
            txtUser.KeyPress += new KeyPressEventHandler(txtUser_KeyPress);
            txtPassword.KeyPress += new KeyPressEventHandler(txtPassword_KeyPress);
            btnLogin.Click += new EventHandler(btnLogin_Click);
            this.Load += new EventHandler(Form1_Load);
            txtUser.Leave += new EventHandler(txtUser_Leave);
            txtPassword.Leave += new EventHandler(txtPassword_Leave);
            txtUser.Enter += new EventHandler(txtUser_Enter);
            txtPassword.Enter += new EventHandler(txtPassword_Enter);
        }

        void txtUser_Enter(object sender, EventArgs e)
        {
            if (txtUser.Text == "Username")
            {
                txtUser.Text = "";
                txtUser.ForeColor = Color.Black;
            }
        }

        void txtPassword_Enter(object sender, EventArgs e)
        {
            if (txtPassword.Text == "Password")
            {
                txtPassword.UseSystemPasswordChar = true;
                txtPassword.Text = "";
                txtPassword.ForeColor = Color.Black;
            }
        }

        void txtPassword_Leave(object sender, EventArgs e)
        {
            if (txtPassword.Text.Length == 0)
            {
                txtPassword.Text = "Password";
                txtPassword.ForeColor = Color.LightGray;
                txtPassword.UseSystemPasswordChar = false;
            }
        }

        void txtUser_Leave(object sender, EventArgs e)
        {
            if (txtUser.Text.Length == 0)
            {
                txtUser.Text = "Username";
                txtUser.ForeColor = Color.LightGray;
            }
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            SearchInfo();
        }

       
        private void SearchInfo()
        {
           
           
            if (txtUser.Text == "")
            {
                MessageBox.Show("Please input your username", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtUser.Focus();
                return;
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("Please input your password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtPassword.Focus();
                return;
            }
            if (txtUser.ForeColor == Color.Black || txtPassword.ForeColor == Color.Black)
            {
                BloodSystem.Query("SELECT * FROM tblusers WHERE Username = '" + txtUser.Text + "' AND Password = '" + txtPassword.Text + "'");
                if (BloodSystem.tbl.Rows.Count > 0)
                {
                    string position = BloodSystem.tbl.Rows[0]["Position"].ToString();
                    Main frm = new Main(txtUser.Text, txtPassword.Text);
                    frm.LevelofAccess(position);
                    this.Hide();
                    frm.ShowDialog();
                    this.Close();
                }

                else
                {
                    txtUser.Clear();
                    txtPassword.Clear();
                    MessageBox.Show("Incorrect Password or Username", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    txtUser.Focus();
                    return;

                }
            }
           
            

            txtUser.Clear();
            txtPassword.Clear();
            
        }

    

      

        void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                SearchInfo();
            }
        }

        void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                txtPassword.Focus();
            }
        }

        void Form1_Load(object sender, EventArgs e)
        {
            txtUser.Focus();
            BloodSystem.Connect();
        }

        void chkShowPass_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShowPass.Checked)
            {
                txtPassword.UseSystemPasswordChar = false;
            }
            else
            {
                if (txtPassword.ForeColor == Color.Black)
                {
                    txtPassword.UseSystemPasswordChar = true;
                }

            }
        }
    }
}
